﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PokerClient.Connection.CommandHandlers;
using PokerClient.Connection.Interfaces;
using PokerClient.Connection.Protocols;
using PokerClient.VisualDisp;
using System.Threading;

namespace PokerClient.VisualDisp
{
	public partial class Lobby : Form,IChangeWindow
	{
		delegate void setCombobox(Dictionary<String, TableIdent> data);
		delegate void setText(String text);
		delegate void chgScr(String text);

		Client myClient;
		String tableid;
		Dictionary<String,TableIdent> tabledata;
		private StatTable stable;
		public Lobby(Client client)
		{
			InitializeComponent();
			this.myClient = client;
			this.myClient.assignLoby = this;
			this.myClient.currentWindow = this;
			this.init();

		}

		private void init()
		{
			this.unameTB.Text = this.myClient.usern;
			this.chipsTB.Text = this.myClient.chip;
			System.Threading.Thread.Sleep(50);
			this.myClient.sent(PokerClient.Connection.Protocols.ProtocolsRequests.requestTableList());		
			//this.printStatusBar(myClient.tableList);
		}

		public Dictionary<String, TableIdent> tables
		{
			set
			{
				this.tabledata = value;
				this.printStatusBar(this.tabledata);
			}
			get
			{
				return this.tabledata;
			}
		}

		public void printStatusBar(Dictionary<String, TableIdent> data)
		{

			if (this.tableBox1.InvokeRequired)
			{
				setCombobox sttxt = new setCombobox(printStatusBar);
				this.tableBox1.Invoke(sttxt, new Object[] { data });
			}
			else
			{
				List<String> dataList = new List<String>(data.Keys);
				dataList.Add(" ");
				this.tableBox1.DataSource = dataList;
				this.tableBox1.Text = " ";
			}

		}

		public void updateStatus(String text)
		{
			if (this.textBox1.InvokeRequired)
			{
				setText sttext = new setText(updateStatus);
				this.textBox1.Invoke(sttext, new object[] { text });
			}
			else
			{
				this.textBox1.Text = text;
			}
		}

		private void tableBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				String temp = tableBox1.Text;
				if (temp != "" && temp != " ")
				{
					TableIdent t;
					tabledata.TryGetValue(temp, out t);
					this.minBuyTB.Text = t.min + "";					
					this.maxBuyTB.Text = t.max + "";
					
					
				}
				else
				{
					this.minBuyTB.Text = " ";
					this.maxBuyTB.Text = " ";
					tableid = " ";
				}
			}
			catch (System.Exception ex)
			{
				this.updateStatus(ex.Message);	
			}
		}



		public void changeScreen(String name)
		{

			if (this.InvokeRequired)
			{
				chgScr cs = new chgScr(changeScreen);
				this.Invoke(cs, new object[] { name });
			}
			else
			{
				if (name.Equals("Table"))
				{
					String s1 = this.maxBuyTB.Text;
					String s2 = this.minBuyTB.Text;
					Table table = new Table(myClient, this,myClient.sTable, s1, s2);
				
					Hide();
					table.Show();
				}
				else
				{
					Show();
					BringToFront();
					Focus();
					myClient.sTable = "-1";
					TopMost = true;
				}
			}
		}

		private void chgwdo()
		{
			Application.Run();
			
		}
		private void button2_Click(object sender, EventArgs e)
		{
			this.myClient.reset();
			this.Dispose();

		}

		private void button1_Click(object sender, EventArgs e)
		{
			String temp = tableBox1.Text;
			if(temp != "" && temp != " ")
			{
				int i = -1;
				try
				{
					i = Int32.Parse(temp);
					myClient.sTable = temp;
					this.tableid = temp;
					myClient.sent(PokerClient.Connection.Protocols.ProtocolJoinTableSet.joinTable(temp));
					System.Threading.Thread.Sleep(200);

				}
				catch (Exception es)
				{
					this.updateStatus(es.Message);
				}
				
			}
		}



		public DataTable dBase 
		{
			set
			{ 
				this.stable.setTable = value ;
			}
		}

		private void statBtn_Click(object sender, EventArgs e)
		{
			if (stable == null || stable.IsDisposed )
			{						
				stable = new StatTable(this.myClient);
				stable.Visible = true;
				stable.Show();
			}
			else
			{
				stable.Show();
			}
		}
	}
}
